/* cl.ext.given.report 0.1*/
(
    function (factory) {
        if (typeof define === 'function' && define.amd) {
            define(['jquery',"echarts","eChartChina","eChartTheme"], function($,echartsjs) {
                factory($,echartsjs);
            });
        } else if (typeof module === 'object' && module.exports) {
            var echartsjs = require('echarts');
            var eChartChinajs = require('eChartChina');
            var eChartThemejs = require('eChartTheme');
            module.exports = function( root, jQuery ) {
                if ( jQuery === undefined ) {
                    if ( typeof window !== 'undefined' ) {
                        jQuery = require('jquery');
                    }
                    else {
                        jQuery = require('jquery')(root);
                    }
                }
                factory(jQuery,echartsjs);
            };
        } else {
            factory(jQuery,echarts);
        }
    }(
    function ($,echarts){
        var defaultExtInfo={};
        var defaults={};
        var $modalCommon={
            getDefaults :function () {
                return defaults
            },
            getOptions : function (elementData,options) {
                options=$.ExtCommon.isCheckEmpty(options)?{}:options;
                var optionBase=$.ExtCommon.initMergeJson(elementData,options);
                optionBase =$.ExtCommon.initMergeJson(this.getDefaults(),optionBase);
                return optionBase
            }
        };
        var ReportExtModal = function(options) {};
        ReportExtModal.prototype = {
            initBind: function (pageOption, listData, cb) {
                var self = this;
                self.pageData = {};
                if(pageOption.chart.infoDefault){
                    var arrTitleName={};
                    pageOption.chart.infoDefault.titleSort=$.map(pageOption.chart.infoDefault.titles,function(info,i){
                        arrTitleName[info.id]=info.name;
                        return info.id
                    });
                    pageOption.chart.infoDefault.titleName=arrTitleName;
                }
                self.pageData.para = pageOption;
                if (!$.ExtCommon.isCheckEmpty(listData)) {
                    var json ={list:listData};
                    self.pageData.original = json;
                    self.pageData.chartdata = json.list.length == 0
                        ? {}
                        : self.structChartDataListToJson(json.list, {
                            name: pageOption.chart.keyword,
                            nameformat: pageOption.type,
                            len: pageOption.chart.xAxisLen,
                            showOther: pageOption.chart.xOther,
                            isDesc: pageOption.chart.isDesc,
                            showIndexName:pageOption.chart.showIndexName,
                            convertName:pageOption.chart.convertName
                        }, pageOption.chart.infoDefault.titleName)
                } else {
                    self.pageData.original = {list: []};
                    self.pageData.chartdata = {};
                }
                if (cb)cb(self.pageData)

            },
            getChartTabsQueue: function (value, json) {
                var info = json.infoChartTabsQueue;
                var list = info.list.arrclone();
                value = value.toString();
                var idx = list.indexOf(value);
                if (idx > -1) {
                    list.splice(idx, 1)
                } else {
                    list.push(value);
                    if (list.length > info.count) {
                        list.splice(0, 1)
                    }
                }
                if (list.length > 0)json.infoChartTabsQueue.list = list;
                return list;
            },
            structChartDataListToJson: function (list, xConfig, yConfig) {
                var xInfo = {name: [], allname: []};
                var yInfo = {};
                var otherInfo = {};
                var dataInfo = [];
                var yArrKey = [];
                for (var yc in yConfig) {
                    yInfo[yc] = {name: yConfig[yc], data: [], alldata: []};
                    yArrKey.push(yc);
                    otherInfo[yc] = 0;
                }
                var xi = 0;
                var xlen = list.length;
                if (xConfig.len > 0) {
                    if (xConfig.isDesc) {
                        xi = list.length > xConfig.len ? list.length - xConfig.len : 0;
                        xlen = list.length;
                    } else {
                        xi = 0;
                        xlen = list.length > xConfig.len ? xConfig.len : list.length
                    }
                }

                $.each(list, function (i, info) {
                    if (i >= xi && i < xlen) {
                        dataInfo.push(info);
                    }
                    for (var k in info) {
                        var value = info[k];
                        if (k == xConfig.name) {
                            xInfo.allname.push(value);
                            if (i >= xi && i < xlen) {
                                var nameValue = value;
                                if(!$.ExtCommon.isCheckEmpty(xConfig.showIndexName) && !$.ExtCommon.isCheckEmpty(xConfig.convertName) && xConfig.showIndexName.indexOf(value.toString())>-1){
                                    nameValue=info[xConfig.convertName]
                                }
                                switch (xConfig.nameformat) {
                                    case "day":
                                        nameValue = $.ExtMoment.initFormat({value: value, format: "MM.DD"});
                                        break;
                                }
                                xInfo.name.push(nameValue);
                            }
                        }
                        if (yArrKey.indexOf(k) > -1) {
                            yInfo[k].alldata.push(value);
                            if (i >= xi && i < xlen) {
                                yInfo[k].data.push(value)
                            } else {
                                otherInfo[k] += info[k];
                            }
                        }
                    }
                });
                return {x: xInfo, y: yInfo, data: dataInfo, otherInfo: otherInfo};
            },
            getChartOption: function (json,options) {
                var self = this;
                var arrJsonKey = self.getCharSeriesKey(json);
                var optionChart = self.getCompositeChartInfo(arrJsonKey, json);
                var chartBaseInfo = self.optionChartBase(options);
                if(!$.ExtCommon.isCheckEmpty(chartBaseInfo.legend)){chartBaseInfo.legend.data = optionChart.legendData;}
                chartBaseInfo.xAxis.data = optionChart.xNames;
                chartBaseInfo.yAxis = optionChart.yaxis;
                chartBaseInfo.series = optionChart.series;
                return chartBaseInfo;
            },
            getCharSeriesKey: function (json) {
                var self = this;
                var paraTypeTopLevel = ["day", "hour"];
                var arrNameLevel=json.para.chart.infoDefault.titleSort;
                var paraType = json.para.type;
                var chartConfig = json.para.chart;
                var arrlist = json.infoChartTabsQueue.list;
                var arrKey = $.map(arrlist, function (name, i) {
                    var type = chartConfig.seriesType;
                    return {name: name, type: type, len: chartConfig.xAxisLen, isShowOther: chartConfig.xOther}
                });
                if(arrKey.length == 2){
                    var k1 = arrNameLevel.indexOf(arrKey[0].name);
                    var k2 = arrNameLevel.indexOf(arrKey[1].name);
                    if(paraTypeTopLevel.indexOf(paraType) == -1){
                        if (k1 > k2) {
                            arrKey[0].type = "line";
                            arrKey[1].type = "bar";
                        } else {
                            arrKey[0].type = "bar";
                            arrKey[1].type = "line"
                        }
                    }
                    if(k1 > k2){arrKey=arrKey.reverse()}
                }
                return arrKey
            },
            getCompositeChartInfo: function (arrjson, json) {
                var self = this;
                var legendData = [];
                var series = [];
                var yaxis = [];
                var xNames = [""];
                var datajson=json.chartdata;
                var titleNames=json.para.chart.infoDefault.titleName;
                var space=$.ExtCommon.isCheckEmpty(json.para.chart.space)?5:json.para.chart.space;
                $.each(arrjson, function (i, arr) {
                    if ($.isEmptyObject(datajson)) {
                        var tempEmptyName = titleNames[arr.name];
                        legendData.push(tempEmptyName);
                        var tempEmptyData = [];
                        for (var a = 0; a < arr.len; a++) tempEmptyData.push(0);
                        if (arr.isShowOther) tempEmptyData.push(0);
                        series.push({name: tempEmptyName, type: arr.type, yAxisIndex: i, data: tempEmptyData});
                        yaxis.push({type: 'value', min: 0, max: 1, interval: 1});
                    } else {
                        var info = datajson.y[arr.name];
                        var tempName = info.name;
                        legendData.push(tempName);
                        var tempData = info.data.arrclone();
                        if (arr.isShowOther && arr.len < info.alldata.length) tempData.push(datajson.otherInfo[arr.name].toDecimal2());
                        series.push({name: tempName, type: arr.type, yAxisIndex: i, data: tempData});
                        var maxratio = i == 1 ? 1.5 : 1;
                        var infoAxis = self.calculateChartScale(tempData, space, maxratio);
                        yaxis.push({type: 'value', min: 0, max: infoAxis.max, interval: infoAxis.interval});
                    }
                });
                if (!$.isEmptyObject(datajson)) {
                    xNames = datajson.x.name.arrclone();
                    if (arrjson[0].isShowOther && arrjson[0].len < datajson.x.allname.length) xNames.push("其他");
                }
                return {legendData: legendData, series: series, yaxis: yaxis, xNames: xNames}
            },
            calculateChartScale: function (arrData, space, maxratio) {
                space = parseInt(space && space > 0 ? space : 5);
                var tempMax = Math.max.apply(null, arrData);
                var tempInterval = maxratio > 1 ? tempMax * maxratio / space : tempMax / space;
                var interval = Math.ceil(tempInterval);
                var max = 1;
                if (tempInterval > 1) {
                    interval = interval + parseInt(tempInterval / 100) * 2;
                } else {
                    interval = (Math.ceil(tempInterval * 100) + 1) / 100
                }
                max = space * interval;
                return {max: max, interval: interval}
            },
            optionChartBase: function (options) {
                //infoExt:{hideLegend,}
                var optionBase={
                    title: {text: ''},
                    tooltip: {trigger: 'axis'},
                    legend: {data: []},
                    grid: {top:"30px",left: '1%', right: '1%', bottom: '1%', containLabel: true},
                    xAxis: {type: 'category', boundaryGap: true, splitLine: {show: false}, data: []},
                    yAxis: [],
                    series: []
                };
                if(!$.ExtCommon.isCheckEmpty(options)){
                    optionBase =$.ExtCommon.initMergeJson(optionBase,options);
                    if(options.infoExt){
                        if(options.infoExt["hideLegend"]){optionBase.legend={};}
                    }
                }
                return optionBase
            },
            getChartListToOptionInfo: function (list, valueKey) {
                var legend = [];
                var seriesData = [];
                var arrValue = [0];
                $.each(list, function (i, info) {
                    legend.push(info.key);
                    seriesData.push({value: info[valueKey], name: info.key});
                    arrValue.push(parseInt(info[valueKey]));
                });
                var maxValue = Math.max.apply(null, arrValue);
                var sortArr = arrValue.arrclone().sort(function (a, b) {
                    return b - a;
                });
                return {legend: legend, seriesData: seriesData, maxValue: maxValue, oldArr: arrValue, sortArr: sortArr}
            },
            initECharts:function(obj,option){
                echarts.init(obj,"macarons").setOption(option);
            }
        };
        $.ExtReport = new ReportExtModal;
        $.ExtReport.Constructor = ReportExtModal;
        return ReportExtModal;
    })
);